﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
using System.Web.Script.Serialization;
using LCA.Ajax;
using Composite.Data;

public partial class Admin_Ajax_UnitService : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        StreamReader sr = new StreamReader(Request.InputStream);

        string str = sr.ReadToEnd();
        var table = new JavaScriptSerializer().Deserialize<dynamic>(str);

        FCResult result = ProcessData(table);
        string strResult = new JavaScriptSerializer().Serialize(result);

        Response.AddHeader("Content-Type", "text/plain");
        Response.Write(strResult);
        Response.Flush();
        Response.End();
    }

    private FCResult ProcessData(dynamic table)
    {
        string opt = table["opt"];
        FCResult result = null;
        switch (opt)
        {
            case "init_unit":
                result = GetAllUnits();
                break;
            case "init_item_unit":
                result = GetAllItemUnits();
                break;

        }

        return result;
    }

    private FCResult GetAllUnits()
    {
        FCResult result = new FCResult();
        try
        {
            dynamic units = null;
            using (DataConnection conn = new DataConnection())
            {
                units = from u in conn.Get<LCA.Data.Unit>()
                        join uc in conn.Get<LCA.Data.UnitGroup>() on u.UnitGroup equals uc.Id
                        select new
                        {
                           UnitId = u.Id,
                           UnitName = uc.Name + " - " + u.Name,
                           UnitGroupId = u.UnitGroup
                        };

            }

            result.Result = true;
            result.Data = new JavaScriptSerializer().Serialize(units); ;
        }
        catch (Exception ex)
        {
            result.Result = false;
            result.Data = ex.Message;
        }

        return result;
    }

    private FCResult GetAllItemUnits()
    {
        FCResult result = new FCResult();
        try
        {
            dynamic items = null;
            using (DataConnection conn = new DataConnection())
            {
                items = (from f in conn.Get<LCA.Data.Flow>()
                             join u in conn.Get<LCA.Data.Unit>() on f.Unit equals u.Id into joined
                             from j in joined.DefaultIfEmpty()
                             where f.IsDeleted == false
                             orderby f.Name
                             select new
                             {
                                 ItemName = f.Name,
                                 ItemType = "Flow",
                                 ItemId = f.Id.ToString().ToLower(),
                                 UnitGroupId = j.UnitGroup.ToString().ToLower(),
                                 UnitId = j.Id.ToString().ToLower()
                             }).ToList();

                var systemItems = (from f in conn.Get<LCA.Data.LCASystem>()
                                 join u in conn.Get<LCA.Data.Unit>() on f.Unit equals u.Id into joined
                                 from j in joined.DefaultIfEmpty()
                                 where f.IsDeleted == false
                                 orderby f.Name
                                 select new
                                 {
                                     ItemName = f.Name,
                                     ItemType = "System",
                                     ItemId = f.Id.ToString().ToLower(),
                                     UnitGroupId = j.UnitGroup.ToString().ToLower(),
                                     UnitId = j.Id.ToString().ToLower()
                                 }).ToList();

                items.AddRange(systemItems);
            }

            result.Result = true;
            result.Data = new JavaScriptSerializer().Serialize(items);
        }
        catch (Exception ex)
        {
            result.Result = false;
            result.Data = ex.Message;
        }

        return result;
    }
}